<template>
  <div class="login-container">
    <div class="loginRow">
      <el-row>
        <el-col :xs="24" :sm="24" :md="12" :lg="16" :xl="16">
          <div style="color: transparent;visibility: hidden;overflow: hidden;">占位符</div>
        </el-col>
        <el-col :xs="24" :sm="24" :md="12" :lg="8" :xl="8">
          <div class="login-content">
            <div class="login-form">
              <div class="login-logo">
                <img :src="LOGO">
              </div>
              <el-form ref="loginForm" :model="loginForm" :rules="loginRules" auto-complete="on" label-position="left">
                <!--用户名【start】-->
                <el-form-item prop="username">
                  <span class="svg-container">
                    <svg-icon icon-class="user" />
                  </span>
                  <el-input
                    ref="username"
                    v-model="loginForm.username"
                    placeholder="请输入用户名"
                    name="username"
                    type="text"
                    tabindex="1"
                    auto-complete="on"
                  />
                </el-form-item>
                <!--用户名【end】-->

                <!--密码【start】-->
                <el-form-item prop="password">
                  <span class="svg-container">
                    <svg-icon icon-class="password" />
                  </span>
                  <el-input
                    :key="passwordType"
                    ref="password"
                    v-model="loginForm.password"
                    :type="passwordType"
                    placeholder="请输入密码"
                    name="password"
                    tabindex="2"
                    auto-complete="on"
                    @keyup.enter.native="handleLogin"
                  />
                  <span class="show-pwd" @click="showPwd">
                    <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
                  </span>
                </el-form-item>
                <!--密码【end】-->

                <el-button :loading="loading" type="primary" style="width:100%;" @click.native.prevent="handleLogin">登录</el-button>
              </el-form>
            </div>
          </div>
        </el-col>
      </el-row>
    </div>

    <!--粒子动画【start】-->
    <vue-particles
      color="#ffffff"
      :particle-opacity="0.7"
      :particles-number="80"
      shape-type="circle"
      :particle-size="4"
      lines-color="#ffffff"
      :lines-width="1"
      :line-linked="true"
      :line-opacity="0.4"
      :lines-distance="150"
      :move-speed="3"
      :hover-effect="true"
      hover-mode="grab"
      :click-effect="true"
      click-mode="push"
    />
    <!--粒子动画【end】-->
  </div>
</template>

<script>
import SettingEnv from '@/setting.env';

export default {
  name: 'Login',
  data() {
    const validateUsername = (rule, value, callback) => {
      if (/[\u4E00-\u9FA5]/g.test(value)) {
        callback(new Error('不能输入汉字'))
      } else if (!value.trim()) {
        callback(new Error('请输入您的账号'))
      } else {
        // 验证通过
        callback()
      }
    }
    const validatePassword = (rule, value, callback) => {
      if (value.trim().length < 6) {
        callback(new Error('密码不能少于6位'))
      } else if (!value.trim()) {
        callback(new Error('请输入您的密码'))
      } else {
        callback()
      }
    }
    return {
      LOGO: require('@/assets/images/login_logo.png'),
      loginForm: {
        username: '',
        password: '',
        activityId:""
      },
      loginRules: {
        username: [{ required: true, trigger: 'blur', validator: validateUsername }],
        password: [{ required: true, trigger: 'blur', validator: validatePassword }]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined,
      hashValue:null,
    }
  },
  created(){
    // 获取完整的 hash 值
    const fullHash = this.$route.hash;
    // 提取所需的部分（假设 hash 值的格式固定，例如 UUID）
    // 这里使用正则表达式来匹配 UUID 形式的字符串
    const regex = /([a-f\d]{8}(-[a-f\d]{4}){3}-[a-f\d]{12}?)/i;
    const match = fullHash.match(regex);
    // 如果匹配成功，则 match[1] 是你想要的部分
    console.log(">>>>>>>>>>>>",match);
    if (match) {
      this.hashValue = match[1];
      this.loginForm.activityId = this.hashValue
    } else {
      this.hashValue = '';
    }
    
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true
          this.$store.dispatch('user/login', this.loginForm).then(() => {
            this.$router.push({ path: this.redirect || `${SettingEnv.routerMode === 'history' ? SettingEnv.customRouter : '/'}` })
            this.loading = false
          }).catch(() => {
            this.loading = false
          })
        } else {
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss">
$bg:#283443;
$light_gray:#fff;
$cursor: #fff;
$caret: #000;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: #606266 !important;
  }
}

.login-container {
  .el-input {
    display: inline-block;
    height: 47px;
    width: 85%;

    input {
      background: transparent;
      border: 0px;
      -webkit-appearance: none;
      border-radius: 0px;
      padding: 12px 5px 12px 15px;
      color: $light_gray;
      height: 47px;
      caret-color: $caret;

      &:-webkit-autofill {
        box-shadow: 0 0 0px 1000px $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: 1px solid rgba(255, 255, 255, 0.1);
    background-color: #f6f4fc;
    border-radius: 5px;
    color: #454545;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  height: 100vh;
  background-color: $bg;
  overflow: hidden;
  background: url(../../assets/images/loginBack.jpg) 50% fixed no-repeat;
  background-size: cover;
  .loginRow {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    z-index: 99;
    .login-content {
      margin: calc(50vh - 249.5px) 10% 10%;
      .login-form {
        margin: auto;
        overflow: hidden;
        z-index: 2;
        padding: 30px 10px;
        border-radius: 6px;
        .login-logo {
          padding: 15px 0 !important;
          box-sizing: border-box !important;
          display: flex;
          justify-content: center;
        }
      }
    }
  }

  .tips {
    font-size: 14px;
    color: #fff;
    margin-bottom: 10px;

    span {
      &:first-of-type {
        margin-right: 16px;
      }
    }
  }

  .svg-container {
    padding: 6px 5px 6px 15px;
    color: $dark_gray;
    vertical-align: middle;
    width: 30px;
    display: inline-block;
    svg {
      fill: #c0c4cc;
    }
  }

  .title-container {
    position: relative;

    .title {
      font-size: 26px;
      font-family: serif;
      color: #000 ;
      margin: 0px auto 40px auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: 10px;
    top: 7px;
    font-size: 16px;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }

  #particles-js {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 1;
  }
}
</style>
